import { ajax, tools } from './ajax.js';

//#region 查询的部分
let tbData = document.getElementById('tbData');
let txtDeptName = document.getElementById('txtDeptName');
let btnQuery = document.getElementById('btnQuery');

btnQuery.addEventListener('click', () => {
  //修改查询条件就必须回到第一页
  page.pageNumber = 1;
  query();
});
//查询条件
let queryInfo = {
  deptName: '',
};

let list = [];
let page = {
  pageSize: 5,
  pageNumber: 1,
  pageCount: 0,
  total: 0,
};

function query() {
  //查询参数处理
  queryInfo.deptName = txtDeptName.value;
  ajax.post(
    '/manage/dept/queryAll',
    tools.mergeJson(page, queryInfo),
    (Data) => {
      if (Data.success) {
        console.log('查询结果；', Data);
        //获取数据之后显示
        list = Data.list;
        showData();
        //分页显示
        page = Data.page;
        showpage();
        return;
      }
      alert(Data.message);
    }
  );
}
let apre = document.getElementById('apre');
let anext = document.getElementById('anext');
let sppage = document.getElementById('sppage');

//下一页
anext.addEventListener('click', () => {
  if (page.pageNumber >= page.pageCount) {
    return;
  }
  page.pageNumber++;
  query();
});
//上一页
apre.addEventListener('click', () => {
  if (page.pageNumber == 1) {
    return;
  }
  page.pageNumber--;
  query();
});

function showpage() {
  sppage.innerHTML = `
当前页码/总页数/记录数：${page.pageNumber}/${page.pageCount}/${page.total}
`;
}

function showData() {
  tbData.innerHTML = '';
  for (let i = 0; i < list.length; i++) {
    let info = list[i];
    let tr = document.createElement('tr');
    let td;
    //编号
    td = document.createElement('td');
    td.append(info.deptId);
    tr.append(td);

    //名称
    td = document.createElement('td');
    td.append(info.deptInfo);
    tr.append(td);

    //描述
    td = document.createElement('td');
    td.append(info.deptName);
    tr.append(td);
    tbData.append(tr);

    //信息修改时间
    td = document.createElement('td');
    td.append(tools.formatDate(info.lastupdate));
    tr.append(td);

    //操作的部分
    td = document.createElement('td');
    //删除的部分
    let btnDel = document.createElement('button');
    td.append(btnDel);
    btnDel.append('删除');
    btnDel.addEventListener('click', () => {
      del(info);
    });

    //修改的部分
    let btnModi = document.createElement('button');
    td.append(btnModi);
    btnModi.append('修改');
    btnModi.addEventListener('click', () => {
      showmodify(info);
    });

    tr.append(td);

    tbData.append(tr);
  }
}

query();
//#endregion

//#region 添加的部分

let aname = document.getElementById('aname');
let ainfo = document.getElementById('ainfo');
let btnAdd = document.getElementById('btnAdd');
let spAdd = document.getElementById('spAdd');

btnAdd.addEventListener('click', () => {
  ajax.post(
    '/manage/dept/add',
    {
      deptName: aname.value,
      deptInfo: ainfo.value,
    },
    (data) => {
      spAdd.innerHTML = data.message;
      query();
    }
  );
});

//#endregion

//#region 删除的部分

function del(info) {
  console.log('要删除的信息', info);
  if (confirm(`是否删除<${info.deptName}>?`)) {
    ajax.post('/manage/dept/delete', { deptId: info.deptId }, (data) => {
      alert(data.message);
      query();
    });
  }
}

//#endregion

//#region 修改的部分
let divModi = document.getElementById('divModi');
let mname = document.getElementById('mname');
let minfo = document.getElementById('minfo');
let btnModi = document.getElementById('btnModi');
let btnClose = document.getElementById('btnClose');
let spModi = document.getElementById('spModi');

divModi.style.display = 'none';

let modifyInfo;

function showmodify(info) {
  //记住要修改
  modifyInfo = info;
  console.log('要修改的信息', info);
  // 初始值
  mname.value = info.deptName;
  minfo.value = info.deptInfo;
  divModi.style.display = 'block';

  btnClose.addEventListener('click', () => {
    divModi.style.display = 'none';
  });

    btnModi.addEventListener('click', () => {
      //获取修改后的信息
      modifyInfo.deptName = mname.value;
      modifyInfo.deptInfo = minfo.value;
      console.log('修改后的信息', modifyInfo);
      ajax.post('/manage/dept/update', modifyInfo, (data) => {
        alert(data.message);
        query();
      });
    });
}

//#endregion

// 按照上课部门管理的功能完成班级信息管理
